{% extends 'base.html' %}
{% load static %}

{% block title %}折旧报表{% endblock %}

{% block extra_css %}
<style>
    .stats-card {
        border-left: 4px solid #007bff;
        transition: transform 0.2s;
    }
    .stats-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    }
    .stats-number {
        font-size: 1.8rem;
        font-weight: bold;
    }
    .stats-label {
        color: #6c757d;
        font-size: 0.9rem;
    }
    .filter-section {
        background: #f8f9fa;
        border-radius: 8px;
        padding: 20px;
        margin-bottom: 20px;
    }
    .table-responsive {
        border-radius: 8px;
        overflow: hidden;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    .depreciation-badge {
        font-size: 0.8rem;
        padding: 4px 8px;
    }
    .value-cell {
        text-align: right;
        font-family: 'Courier New', monospace;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h2><i class="fas fa-chart-line me-2"></i>折旧报表</h2>
                <div class="btn-group">
                    <a href="{% url 'assets:report_list' %}" class="btn btn-outline-secondary">
                        <i class="fas fa-arrow-left me-1"></i>返回报表列表
                    </a>
                </div>
            </div>

            <!-- 统计卡片 -->
            <div class="row mb-4">
                <div class="col-md-4">
                    <div class="card stats-card h-100">
                        <div class="card-body text-center">
                            <div class="stats-number text-primary">¥{{ total_original_value|floatformat:2 }}</div>
                            <div class="stats-label">原始价值总计</div>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card stats-card h-100">
                        <div class="card-body text-center">
                            <div class="stats-number text-success">¥{{ total_current_value|floatformat:2 }}</div>
                            <div class="stats-label">当前价值总计</div>
                        </div>
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="card stats-card h-100">
                        <div class="card-body text-center">
                            <div class="stats-number text-danger">¥{{ total_depreciation|floatformat:2 }}</div>
                            <div class="stats-label">累计折旧总计</div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 筛选区域 -->
            <div class="filter-section">
                <form method="get" class="row g-3">
                    <div class="col-md-3">
                        <label class="form-label">资产类别</label>
                        <select name="category" class="form-select">
                            <option value="">全部类别</option>
                            {% for category in categories %}
                                <option value="{{ category.id }}" 
                                        {% if category.id|stringformat:"s" == selected_category %}selected{% endif %}>
                                    {{ category.name }}
                                </option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">所属部门</label>
                        <select name="department" class="form-select">
                            <option value="">全部部门</option>
                            {% for department in departments %}
                                <option value="{{ department.id }}" 
                                        {% if department.id|stringformat:"s" == selected_department %}selected{% endif %}>
                                    {{ department.name }}
                                </option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">折旧状态</label>
                        <select name="depreciation_status" class="form-select">
                            {% for value, label in depreciation_status_choices %}
                                <option value="{{ value }}" 
                                        {% if value == selected_depreciation_status %}selected{% endif %}>
                                    {{ label }}
                                </option>
                            {% endfor %}
                        </select>
                    </div>
                    <div class="col-md-3">
                        <label class="form-label">&nbsp;</label>
                        <button type="submit" class="btn btn-primary w-100">
                            <i class="fas fa-filter me-1"></i>筛选
                        </button>
                    </div>
                </form>
            </div>

            <!-- 资产折旧列表 -->
            <div class="card">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0"><i class="fas fa-list me-2"></i>资产折旧详情</h5>
                    <small class="text-muted">共 {{ assets|length }} 条记录</small>
                </div>
                <div class="card-body p-0">
                    {% if assets %}
                    <div class="table-responsive">
                        <table class="table table-hover mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th>资产信息</th>
                                    <th>购买价格</th>
                                    <th>当前价值</th>
                                    <th>累计折旧</th>
                                    <th>折旧率</th>
                                    <th>折旧状态</th>
                                    <th>购买日期</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {% for asset in assets %}
                                <tr>
                                    <td>
                                        <div>
                                            <strong>{{ asset.name }}</strong>
                                            <br>
                                            <small class="text-muted">{{ asset.asset_number }} - {{ asset.category.name|default:"-" }}</small>
                                        </div>
                                    </td>
                                    <td class="value-cell">
                                        <span class="fw-bold text-primary">
                                            ¥{{ asset.purchase_price|floatformat:2|default:"-" }}
                                        </span>
                                    </td>
                                    <td class="value-cell">
                                        <span class="fw-bold text-success">
                                            ¥{{ asset.current_value|floatformat:2 }}
                                        </span>
                                    </td>
                                    <td class="value-cell">
                                        <span class="fw-bold text-danger">
                                            ¥{{ asset.depreciation_amount|floatformat:2 }}
                                        </span>
                                    </td>
                                    <td class="text-center">
                                        <span class="badge bg-info">
                                            {{ asset.depreciation_rate|floatformat:1 }}%
                                        </span>
                                    </td>
                                    <td>
                                        {% if asset.is_fully_depreciated %}
                                            <span class="badge bg-secondary depreciation-badge">已完全折旧</span>
                                        {% elif asset.depreciation_amount > 0 %}
                                            <span class="badge bg-warning depreciation-badge">部分折旧</span>
                                        {% else %}
                                            <span class="badge bg-light text-dark depreciation-badge">无折旧</span>
                                        {% endif %}
                                    </td>
                                    <td>{{ asset.purchase_date|date:"Y-m-d"|default:"-" }}</td>
                                    <td>
                                        <a href="{% url 'assets:depreciation_schedule' asset.id %}" 
                                           class="btn btn-sm btn-outline-primary" title="查看折旧计划">
                                            <i class="fas fa-calendar-alt"></i>
                                        </a>
                                    </td>
                                </tr>
                                {% endfor %}
                            </tbody>
                        </table>
                    </div>
                    {% else %}
                    <div class="text-center py-5">
                        <i class="fas fa-inbox fa-3x text-muted mb-3"></i>
                        <p class="text-muted">暂无符合条件的资产记录</p>
                        {% if selected_category or selected_department or selected_depreciation_status %}
                        <a href="{% url 'assets:depreciation_report' %}" class="btn btn-outline-primary">
                            <i class="fas fa-times me-1"></i>清除筛选条件
                        </a>
                        {% endif %}
                    </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
// 自动提交表单当选择框改变时
document.addEventListener('DOMContentLoaded', function() {
    const selects = document.querySelectorAll('select[name="category"], select[name="department"], select[name="depreciation_status"]');
    selects.forEach(select => {
        select.addEventListener('change', function() {
            this.form.submit();
        });
    });
});
</script>
{% endblock %}